package com.example.mywechat;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private BluetoothAdapter mBluetoothAdapter;
    private ChatService mChatService = null;
    //生产4个Fragment控制器
    private Fragment fragmentWeiXin = new WeiXinFragment();
    private Fragment fragmentFriend = new FriendFragment();
    private Fragment fragmentAddress = new AddressFragment();
    private Fragment fragmentSetting = new SettingFragment();

    //四个按钮
    private LinearLayout buttonWeiXin;
    private LinearLayout buttonFriend;
    private LinearLayout buttonAddress;
    private LinearLayout buttonSetting;

    private ImageView imageButtonWeiXin;
    private ImageView ImageButtonFriend;
    private ImageView ImageButtonAddress;
    private ImageView ImageButtonSetting;

    //表示Fragment进行通讯的控制器
    private FragmentManager fm;


    // 定义数据库访问对象
    private MyDAO myDAO;
    private List<Map<String,Object>> listData;
    private Map<String,Object> listItem;
    // 定义字段
    private EditText et_Name = null;
    private EditText et_Tel = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initFragment();
        initEvent();
        setFragment(0);
        initDAO();
        initListPhone();
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        // 若当前设备不支持蓝牙功能
        if(mBluetoothAdapter == null){
            Toast.makeText(this,"蓝牙不可用",Toast.LENGTH_LONG).show();
            finish();
            return;
        }
    }

//    @Override
//    public void onStart(){
//        super.onStart();
//        if(!mBluetoothAdapter.isEnabled()){
//            // 若当前设备蓝牙功能未开启，则开启蓝牙
//            Intent enableIntent = new Intent(
//                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
//            startActivityForResult(enableIntent,REQUEST_ENABLE_BT);
//        } else{
//            if(mChatService==null)
//                setupChat();
//        }
//    }

    private void initView(){
        buttonWeiXin = findViewById(R.id.id_button_weixin);
        buttonFriend = findViewById(R.id.id_button_friend);
        buttonAddress =  findViewById(R.id.id_button_address);
        buttonSetting =  findViewById(R.id.id_button_setting);

        imageButtonWeiXin = findViewById(R.id.id_button_weixin_img);
        ImageButtonFriend = findViewById(R.id.id_button_friend_img);
        ImageButtonAddress = findViewById(R.id.id_button_address_img);
        ImageButtonSetting = findViewById(R.id.id_button_setting_img);
    }

    private void initDAO(){
        myDAO = new MyDAO(this);
        if(myDAO.getRecordsNumber()==0){
            myDAO.insertInfo("tian", "13012345678");
            myDAO.insertInfo("wang","13076543210");
            myDAO.insertInfo("zhang","13471234567");
            myDAO.insertInfo("li","15971234567");
            myDAO.insertInfo("ming","13012345678");
            myDAO.insertInfo("liu","13012345678");
            myDAO.insertInfo("wu","13012345678");
            myDAO.insertInfo("han","13012345678");
        }
    }
    private void initListPhone(){
        listData = new ArrayList<Map<String,Object>>();
        Cursor cursor = myDAO.allQuery();
        while(cursor.moveToNext()){
            String name = cursor.getString(1);
            String tel = cursor.getString(2);
            listItem = new HashMap<String,Object>();
            listItem.put("name", name);
            listItem.put("tel", tel);
            listData.add(listItem);
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            System.out.println("返回来了！需要刷新了！");
            initPhoneView();
            setFragment(2);
    }
    private void initPhoneView(){
        VerticalAdapter adapter = new VerticalAdapter(this);

        RecyclerView rcvVertical  = findViewById(R.id.recycler_view_phone_list);

        LinearLayoutManager managerVertical = new LinearLayoutManager(this);
        managerVertical.setOrientation(LinearLayoutManager.VERTICAL);

        rcvVertical.setLayoutManager(managerVertical);
        rcvVertical.setHasFixedSize(true);
        rcvVertical.setAdapter(adapter);

        adapter.setVerticalDataList(listData);
    }

    private void initFragment(){
        fm = getSupportFragmentManager();
        // 控制器实例
        FragmentTransaction transaction = fm.beginTransaction();
        // R.id.id_content -> 选定主布局中的FrameLayout
        transaction.add(R.id.main_content, fragmentWeiXin);
        transaction.add(R.id.main_content, fragmentFriend);
        transaction.add(R.id.main_content, fragmentAddress);
        transaction.add(R.id.main_content, fragmentSetting);
        transaction.commit();
    }

    private void initEvent(){
        buttonWeiXin.setOnClickListener(this);
        buttonFriend.setOnClickListener(this);
        buttonAddress.setOnClickListener(this);
        buttonSetting.setOnClickListener(this);
    }

    private void setFragment(int i){
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        // 把图片设置为亮
        // 设置内容区域
        switch (i) {
            case 0:
                Log.d("setSelect","1");
                transaction.show(fragmentWeiXin);
                imageButtonWeiXin.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            case 1:
                transaction.show(fragmentFriend);
                ImageButtonFriend.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case 2:
                transaction.show(fragmentAddress);
                ImageButtonAddress.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 3:
                transaction.show(fragmentSetting);
                ImageButtonSetting.setImageResource(R.drawable.tab_settings_pressed);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(fragmentWeiXin);
        transaction.hide(fragmentFriend);
        transaction.hide(fragmentAddress);
        transaction.hide(fragmentSetting);
    }

    private void resetImg(){
        imageButtonWeiXin.setImageResource(R.drawable.tab_weixin_normal);
        ImageButtonFriend.setImageResource(R.drawable.tab_find_frd_normal);
        ImageButtonAddress.setImageResource(R.drawable.tab_address_normal);
        ImageButtonSetting.setImageResource(R.drawable.tab_settings_normal);
    }

    @Override
    public void onClick(View v) {
        Log.d("onClick","1");
        resetImg();
        switch (v.getId()){
            case R.id.id_button_weixin:
                Log.d("onClick_WeiXin","0");
                setFragment(0);
                break;
            case R.id.id_button_friend:
                Log.d("onClick_Frd","1");
                setFragment(1);
                break;
            case R.id.id_button_address:
                Log.d("onClick_contact","2");
                initPhoneView();
                setFragment(2);
                break;
            case R.id.id_button_setting:
                Log.d("onClick_settings","3");
                setFragment(3);
                break;
            default:
                break;
        }
    }
}
